package com.dalianpai.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WGR
 * @create 2020/8/6 -- 15:49
 */
@Configuration
public class MyRabbitConfig {


    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    //声明队列
    @Bean("stockDelayQueue")
    public Queue stockDelayQueue(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","stock-event-exchange");
        arguments.put("x-dead-letter-routing-key","stock.release");
        arguments.put("x-message-ttl",120000);

        //谷粒视频中是new出来的，这个相当于封装了一下，用起来更方便
        return QueueBuilder.durable("stock.delay.queue").withArguments(arguments).build();
    }

    @Bean("stockReleaseStockQueue")
    public Queue stockReleaseStockQueue(){
        return QueueBuilder.durable("stock.release.stock.queue").build();
    }

    @Bean("stockEventExchange")
    public Exchange stockEventExchange(){
        return ExchangeBuilder.topicExchange("stock-event-exchange").durable(true).build();
    }

    //绑定队列和交换机
    @Bean
    public Binding stockLockedBinding(@Qualifier("stockDelayQueue") Queue queue,
                                       @Qualifier("stockEventExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange)
                .with("stock.locked").noargs();
    }

    //绑定队列和交换机
    @Bean
    public Binding stockReleaseBing(@Qualifier("stockReleaseStockQueue") Queue queue,
                                              @Qualifier("stockEventExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange)
                .with("stock.release.#").noargs();
    }
}
